package cn.hahah.module.blog.controller.admin.userdialoguepersonal;

import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.service.users.UsersService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.userdialoguepersonal.vo.*;
import cn.hahah.module.blog.dal.dataobject.userdialoguepersonal.UserDialoguePersonalDO;
import cn.hahah.module.blog.service.userdialoguepersonal.UserDialoguePersonalService;

@Tag(name = "管理后台 - 用户对话个人")
@RestController
@RequestMapping("/blog/user-dialogue-personal")
@Validated
public class UserDialoguePersonalController {

    @Resource
    private UserDialoguePersonalService userDialoguePersonalService;
    @Resource
    private UsersService usersService;

    @PostMapping("/create")
    @Operation(summary = "创建用户对话个人")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue-personal:create')")
    public CommonResult<Long> createUserDialoguePersonal(@Valid @RequestBody UserDialoguePersonalSaveReqVO createReqVO) {
        return success(userDialoguePersonalService.createUserDialoguePersonal(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户对话个人")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue-personal:update')")
    public CommonResult<Boolean> updateUserDialoguePersonal(@Valid @RequestBody UserDialoguePersonalSaveReqVO updateReqVO) {
        userDialoguePersonalService.updateUserDialoguePersonal(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户对话个人")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue-personal:delete')")
    public CommonResult<Boolean> deleteUserDialoguePersonal(@RequestParam("id") Long id) {
        userDialoguePersonalService.deleteUserDialoguePersonal(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户对话个人")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue-personal:query')")
    public CommonResult<UserDialoguePersonalRespVO> getUserDialoguePersonal(@RequestParam("id") Long id) {
        UserDialoguePersonalDO userDialoguePersonal = userDialoguePersonalService.getUserDialoguePersonal(id);
        return success(BeanUtils.toBean(userDialoguePersonal, UserDialoguePersonalRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户对话个人分页")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue-personal:query')")
    public CommonResult<PageResult<UserDialoguePersonalRespVO>> getUserDialoguePersonalPage(@Valid UserDialoguePersonalPageReqVO pageReqVO) {
        PageResult<UserDialoguePersonalDO> pageResult = userDialoguePersonalService.getUserDialoguePersonalPage(pageReqVO);
        PageResult<UserDialoguePersonalRespVO> result = BeanUtils.toBean(pageResult, UserDialoguePersonalRespVO.class);
        for (UserDialoguePersonalRespVO userDialoguePersonalRespVO : result.getList()) {
            UsersDO usersDO = usersService.getUsers(userDialoguePersonalRespVO.getUserId());

            if( usersDO != null){
                userDialoguePersonalRespVO.setUserName(usersDO.getUsername());
            }
        }

        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户对话个人 Excel")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue-personal:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserDialoguePersonalExcel(@Valid UserDialoguePersonalPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserDialoguePersonalDO> list = userDialoguePersonalService.getUserDialoguePersonalPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户对话个人.xls", "数据", UserDialoguePersonalRespVO.class,
                        BeanUtils.toBean(list, UserDialoguePersonalRespVO.class));
    }

}